import wepy from 'wepy'
import { connect } from 'wepy-redux'
import { GlobalApi } from '../api'
import Wx from '../common/wxApi'


@connect({
  userInfo (state) {
    return state.counter.userInfo
  },
  globalData (state) {
    return state.counter.globalData
  },
  sys (state) {
    return state.counter.sys
  }
})
export default class Mixin extends wepy.mixin {
  data = {
    onErrorImg:'https://neverland-32495c.tcb.qcloud.la/myImg/cover4.jpg?sign=8840bcea45c3a80a783f29229551dc07&t=1542267468'
  }
  methods = {
    onImgError(e){
      this.img = this.onErrorImg;
      this.$apply();
    },
    // 配合使用 button.login-btn(wx:if="{{!userInfo}}" open-type="getUserInfo" bindgetuserinfo="onGotUserInfo" data-fn="loginCallBack")
    onGotUserInfo(e) {
      console.log(e.detail)
      const jsonData = e.detail;
      Wx.promiseApi(wx.getSetting)
      .then(res=>{
        if(!res.authSetting['scope.userInfo'])
        {
          wepy.showToast({
            title: '请授权登录', 
            icon: 'none', 
          })
          return
        }
        else
        {
          if (!this.userInfo) {
            GlobalApi.getUnionId({session_key:null,encryptedData: jsonData.encryptedData,iv:jsonData.iv}).then(data => {
              if (data && e.target.dataset.fn) {
                this[e.target.dataset.fn]()
              }
            })
          }
        }
      })
      .catch(err=>{
        console.log(error)
      })
      
    }
  }
}
